// APDUs.cpp: implementation of the APDUs class.
//
//////////////////////////////////////////////////////////////////////

#include "APDUs.h"
#include "logger.h"

int SKF_WRITE_LOG_FILE = 0;
int sv_Device = -1;
unsigned char KEY_HANDLE[SIZE_BUFFER_32] = { '\0' };
CHAR SV_PSZLOGPATH[SIZE_BUFFER_128] = { '\0' };

void WriteLogToFile( CHAR* szLog )
{
    FILE* pFileLog;
    if (SKF_WRITE_LOG_FILE) {
        pFileLog = fopen( SV_PSZLOGPATH, "a+");
        if( pFileLog != NULL ) {
            LOGI("WriteLogToFile ok: %s", szLog);
            fprintf( pFileLog, "log: %s\n", szLog );
            fflush(pFileLog);
            fclose( pFileLog );
        } else {
            LOGE("%s, fopen(w) %s fail, err: %d", __func__, SV_PSZLOGPATH, errno);
        }
    }
}

BYTE bRandomKey[16] = {0};
BYTE bKeyHandle[32] = {0};

BYTE bEccPrikey[32] = {
        0xB1,0xE7,0xFD,0xCB,0x32,0x12,0x1C,0x67,0x3A,0xB7,0x99,0xE5,0xED,0x7B,0xD7,0x86,
        0x60,0xA3,0xA1,0x54,0x30,0x55,0xDB,0x4A,0x0D,0x94,0xD0,0xEF,0xB6,0x98,0x56,0x73
};
BYTE bEccPubkey[64] = {
        0xF0,0x80,0x36,0x1D,0x43,0xE6,0x5B,0x47,0xE8,0xF0,0xD2,0xC1,0x5E,0x99,0x98,0x5E,
        0xD7,0x86,0xED,0x29,0x30,0x8D,0xFF,0xAB,0xB5,0xF0,0x43,0x21,0x6A,0xD6,0x87,0xC2,
        0x50,0x73,0x3E,0x09,0xE0,0x1A,0x48,0xF3,0xBA,0xA5,0xCD,0x7E,0x90,0x35,0xFD,0x76,
        0x6C,0xEB,0x7B,0xFD,0x4D,0x23,0x48,0xA2,0x66,0x94,0x2D,0xBC,0x10,0xE4,0x84,0x56
};

//CA
BYTE APDU_CA_FID[2] = { 0xDD, 0xF5 };
BYTE APDU_MF_FID[2] = { 0x3F, 0x00 };
BYTE APDU_EF01_FID[2] = { 0xEF, 0x01 };
BYTE APDU_EF02_FID[2] = { 0xEF, 0x02 };

BYTE apdu_selectDF[0x07] = { 0x00, 0xA4, 0x00, 0x00, 0x02, 0x00, 0x00 };
BYTE apdu_readBinary[0x05] = {0x00, 0xB0, 0x80, 0x00, 0x00 };
BYTE apdu_updateBinary[0x05] = { 0x00, 0xD6, 0x80, 0x00, 0x00 };


//Random, 0x05
BYTE apdu_random2[0x05] = { 0x00, 0xE8, 0x00, 0x00, 0x00 };
//Response, 0x05
BYTE apdu_getResponse[0x05] = { 0x00, 0xC0, 0x00, 0x00, 0x00 };

//ECC signature key pair
BYTE apdu_GenEccKeyPair[0x08] = { 0x01, 0x07, 0xA0, 0x01, 0xA1, 0x01, 0x01, 0x00 };
BYTE apdu_eccGenKeyPair[0x05] = { 0x80, 0xCE, 0x01, 0x00, 0x02 };
BYTE apdu_importEcc46[0x07] = { 0x46, 0x01, 0x07, 0xA0, 0x02, 0x00, 0x40 };
BYTE apdu_importEcc26[0x07] = { 0x26, 0x02, 0x07, 0xA1, 0x02, 0x00, 0x20 };
BYTE apdu_eccSignData[0x05]   = { 0x80, 0xFA, 0x00, 0x00, 0x28 };
BYTE apdu_eccSignVerify[0x05] = { 0x80, 0xFA, 0x00, 0x00, 0x6A };
BYTE apdu_genDataKeyEcc[0x0D] = { 0x80, 0xC8, 0x00, 0x00, 0x08, 0x01, 0x07, 0xB0, 0x01, 0xB1, 0x01, 0x01, 0x00 };
BYTE apdu_getDevInfo[0x05] = { 0x80, 0xCA, 0x05, 0x00, 0x00 };
BYTE apdu_eccEncrypt[0x05]   = { 0x80, 0xC6, 0x00, 0x00, 0x40 };
BYTE apdu_eccDecrypt[0x05] = { 0x80, 0xC8, 0x00, 0x00, 0x40 };
BYTE apdu_connect[0x07] = { 0x06, 0x74, 0x6D, 0x73, 0x73, 0x69, 0x6D };
BYTE apdu_84_00[0x05] = { 0x00, 0x84, 0x00, 0x00, 0x10 };
BYTE apdu_A4_04[0x04] = { 0x00, 0xA4, 0x04, 0x00 };
BYTE apdu_A5_00[0x05] = { 0x00, 0xA5, 0x00, 0x00, 0x02 };
BYTE apdu_B0_00[0x04] = { 0x00, 0xB0, 0x00, 0x00 };
BYTE apdu_C1_02[0x05] = { 0x80, 0xC1, 0x00, 0x00, 0x02 };
BYTE apdu_C8_00[0x05] = { 0x80, 0xC8, 0x00, 0x00, 0x08 };
BYTE apdu_C8_06[0x06] = { 0x80, 0xC8, 0x00, 0x00, 0x06, 0x00 };
BYTE apdu_C6_A0[0x05] = { 0x80, 0xC6, 0x00, 0x00, 0xA0 };
BYTE apdu_CA_05[0x05] = { 0x80, 0xCA, 0x05, 0x00, 0x00 };
BYTE apdu_CC_00[0x04] = { 0x80, 0xCC, 0x00, 0x00 };
BYTE apdu_CE_00[0x04] = { 0x80, 0xCE, 0x00, 0x00 };
BYTE apdu_CE_01[0x05] = { 0x80, 0xCE, 0x01, 0x00, 0x02 };
BYTE apdu_D1_02[0x05] = { 0x80, 0xD1, 0x00, 0x00, 0x02 };
BYTE apdu_D6_00[0x04] = { 0x00, 0xD6, 0x00, 0x00 };
BYTE apdu_E1_00[0x05] = { 0x80, 0xE1, 0x00, 0x00, 0x20 };
BYTE apdu_F1_00[0x04] = { 0x80, 0xF1, 0x00, 0x00 };
BYTE apdu_F1_40[0x05] = { 0x80, 0xF1, 0x00, 0x00, 0x40 };
BYTE apdu_F4_00[0x04] = { 0x80, 0xF4, 0x00, 0x00 };
BYTE apdu_F8_01[0x04] = { 0x80, 0xF8, 0x01, 0x00 };
BYTE apdu_F8_02[0x04] = { 0x80, 0xF8, 0x02, 0x00 };
BYTE apdu_F8_03[0x04] = { 0x80, 0xF8, 0x03, 0x00 };
BYTE apdu_FA_00[0x04] = { 0x80, 0xFA, 0x00, 0x00 };
BYTE apdu_FA_01[0x05] = { 0x80, 0xFA, 0x01, 0x00, 0x06 };
BYTE apdu_FA_02[0x04] = { 0x80, 0xFA, 0x02, 0x00 };
BYTE apdu_FA_03[0x04] = { 0x80, 0xFA, 0x03, 0x00 };
BYTE apdu_FA_06[0x05] = { 0x80, 0xFA, 0x00, 0x00, 0x06 };
BYTE apdu_FC_01[0x06] = { 0x80, 0xFC, 0x01, 0x00, 0x01, 0x02 };
BYTE apdu_FC_02[0x04] = { 0x80, 0xFC, 0x02, 0x00 };
BYTE apdu_FC_03[0x04] = { 0x80, 0xFC, 0x03, 0x00 };
BYTE apdu_A001[0x02] = { 0xA0, 0x01 };
BYTE apdu_A002[0x02] = { 0xA0, 0x02 };
BYTE apdu_A101[0x02] = { 0xA1, 0x01 };
BYTE apdu_A102[0x02] = { 0xA1, 0x02 };
BYTE apdu_0001[0x02] = { 0x00, 0x01 };
BYTE apdu_0002[0x02] = { 0x00, 0x02 };
BYTE apdu_0010[0x02] = { 0x00, 0x10 };
BYTE apdu_0020[0x02] = { 0x00, 0x20 };
BYTE apdu_0040[0x02] = { 0x00, 0x40 };
BYTE apdu_0800[0x02] = { 0x08, 0x00 };
BYTE apdu_0107[0x02] = { 0x01, 0x07 };
BYTE apdu_0200[0x02] = { 0x02, 0x00 };
BYTE apdu_0207[0x02] = { 0x02, 0x07 };
BYTE apdu_2007[0x02] = { 0x20, 0x07 };
BYTE apdu_00[0x01] = { 0x00 };
BYTE apdu_01[0x01] = { 0x01 };
BYTE apdu_02[0x01] = { 0x02 };
BYTE apdu_03[0x01] = { 0x03 };
BYTE apdu_04[0x01] = { 0x04 };
BYTE apdu_06[0x01] = { 0x06 };
BYTE apdu_08[0x01] = { 0x08 };
BYTE apdu_16[0x01] = { 0x16 };
BYTE apdu_20[0x01] = { 0x20 };
BYTE apdu_26[0x01] = { 0x26 };
BYTE apdu_46[0x01] = { 0x46 };
BYTE apdu_80[0x01] = { 0x80 };
BYTE apdu_FF[0x01] = { 0xFF };

//SM1, encrypt, ECB model, 0x05
BYTE apdu_encrypt_sm1_ecb[0x05] = { 0x80, 0xE2, 0x00, 0x00, 0x20 };
//SM1, decrypt, ECB model, 0x05
BYTE apdu_decrypt_sm1_ecb[0x05] = { 0x80, 0xEA, 0x00, 0x00, 0x20 };
//SM1, encrypt, CBC model, 0x05
BYTE apdu_encrypt_sm1_cbc[0x05] = { 0x80, 0xE2, 0x00, 0x01, 0x20 };
//SM1, decrypt, CBC model, 0x05
BYTE apdu_decrypt_sm1_cbc[0x05] = { 0x80, 0xEA, 0x00, 0x01, 0x20 };
//SM1, encrypt, CFB model, 0x05
BYTE apdu_encrypt_sm1_cfb[0x05] = { 0x00, 0x00, 0x00, 0x00, 0x00 };
//SM1, decrypt, CFB model, 0x05
BYTE apdu_decrypt_sm1_cfb[0x05] = { 0x00, 0x00, 0x00, 0x00, 0x00 };
//SM1, encrypt, OFB model, 0x05
BYTE apdu_encrypt_sm1_ofb[0x05] = { 0x00, 0x00, 0x00, 0x00, 0x00 };
//SM1, decrypt, OFB model, 0x05
BYTE apdu_decrypt_sm1_ofb[0x05] = { 0x00, 0x00, 0x00, 0x00, 0x00 };

//SSF33, encrypt, ECB model, 0x05
BYTE apdu_encrypt_ssf33_ecb[0x05] = { 0x80, 0xE6, 0x00, 0x00, 0x20 };
//SSF33, decrypt, ECB model, 0x05
BYTE apdu_decrypt_ssf33_ecb[0x05] = { 0x80, 0xE8, 0x00, 0x00, 0x20 };
//SSF33, encrypt, CBC model, 0x05
BYTE apdu_encrypt_ssf33_cbc[0x05] = { 0x80, 0xE6, 0x00, 0x01, 0x20 };
//SSF33, decrypt, CBC model, 0x05
BYTE apdu_decrypt_ssf33_cbc[0x05] = { 0x80, 0xE8, 0x00, 0x01, 0x20 };
//SSF33, encrypt, CFB model, 0x05
BYTE apdu_encrypt_ssf33_cfb[0x05] = { 0x00, 0x00, 0x00, 0x00, 0x00 };
//SSF33, decrypt, CFB model, 0x05
BYTE apdu_decrypt_ssf33_cfb[0x05] = { 0x00, 0x00, 0x00, 0x00, 0x00 };
//SSF33, encrypt, OFB model, 0x05
BYTE apdu_encrypt_ssf33_ofb[0x05] = { 0x00, 0x00, 0x00, 0x00, 0x00 };
//SSF33, decrypt, OFB model, 0x05
BYTE apdu_decrypt_ssf33_ofb[0x05] = { 0x00, 0x00, 0x00, 0x00, 0x00 };

//SM4, encrypt, ECB model, 0x05
BYTE apdu_encrypt_sm4_ecb[0x05] = { 0x80, 0xD2, 0x00, 0x00, 0x20 };
//SM4, decrypt, ECB model, 0x05
BYTE apdu_decrypt_sm4_ecb[0x05] = { 0x80, 0xD6, 0x00, 0x00, 0x20 };
//SM4, encrypt, CBC model, 0x05
BYTE apdu_encrypt_sm4_cbc[0x05] = { 0x80, 0xD2, 0x00, 0x01, 0x20 };
//SM4, decrypt, CBC model, 0x05
BYTE apdu_decrypt_sm4_cbc[0x05] = { 0x80, 0xD6, 0x00, 0x01, 0x20 };
//SM4, encrypt, CFB model, 0x05
BYTE apdu_encrypt_sm4_cfb[0x05] = { 0x00, 0x00, 0x00, 0x00, 0x00 };
//SM4, decrypt, CFB model, 0x05
BYTE apdu_decrypt_sm4_cfb[0x05] = { 0x00, 0x00, 0x00, 0x00, 0x00 };
//SM4, encrypt, OFB model, 0x05
BYTE apdu_encrypt_sm4_ofb[0x05] = { 0x00, 0x00, 0x00, 0x00, 0x00 };
//SM4, decrypt, OFB model, 0x05
BYTE apdu_decrypt_sm4_ofb[0x05] = { 0x00, 0x00, 0x00, 0x00, 0x00 };

//group algorithm SM1, SSF33 and SM4, etc. CBC model, 0x05
BYTE apdu_cbc_sendIV[0x05] = { 0x80, 0xD1, 0x00, 0x00, 0x10 };
//SM3 digest
BYTE apdu_sm3_digest[0x05] = { 0x80, 0xB6, 0x00, 0x00, 0x40 };

